//
//  HomeBannerCollectionViewCell.m
//  YYZB
//
//  Created by 顾悦 on 16/3/23.
//
//

#import "HomeBannersCollectionViewCell.h"

#import "HomeBannerCollectionViewCell.h"

#import "UIImageView+OnlineImage.h"

@interface HomeBannersCollectionViewCell () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation HomeBannersCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.pageControl.userInteractionEnabled = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeBannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"bannerCell"];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerTicked) userInfo:nil repeats:YES];
    self.pageControl.numberOfPages = 0;
}

- (void)timerTicked {
    NSUInteger value = self.pageControl.currentPage;
    if (value >= self.pageControl.numberOfPages - 1) {
        value = 0;
    } else {
        value += 1;
    }
    [self pageControlValueChanged:value];
}

- (void)pageControlValueChanged:(NSUInteger)value {
    self.pageControl.currentPage = value;
    CGFloat offsetX = self.collectionView.bounds.size.width * self.pageControl.currentPage;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (void)setBanners:(NSArray<Banner *> *)banners {
    self.pageControl.numberOfPages = [banners count];
    if (_banners == banners) {
        return;
    }
    _banners = banners;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.banners count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bannerCell" forIndexPath:indexPath];
    Banner *b = self.banners[indexPath.item];
    [cell.imageView setOnlineImage:b.imagePath];
    [cell.imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    cell.imageView.contentMode =  UIViewContentModeScaleAspectFill;
    cell.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    cell.imageView.clipsToBounds  = YES;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return collectionView.bounds.size;
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}


@end
